<?php
/* 学校详情页 */
define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

assign_template();


$page = isset($_REQUEST['page'])   && intval($_REQUEST['page'])  > 0 ? intval($_REQUEST['page'])  : 1;
$area_id = isset($_REQUEST['area_id']) && intval($_REQUEST['area_id']) > 0 ? intval($_REQUEST['area_id']) : 0;
$price_max = isset($_REQUEST['price_max']) && intval($_REQUEST['price_max']) > 0 ? intval($_REQUEST['price_max']) : 0;
$donate = isset($_REQUEST['donate']) && intval($_REQUEST['donate']) > 0 ? intval($_REQUEST['donate']) : '';
$donate = isset($_REQUEST['chinese']) && intval($_REQUEST['chinese']) > 0 ? intval($_REQUEST['chinese']) : '';


if($_REQUEST['act']=='list'){


    if($_REQUEST['area_id']){
        $sql =" select parent_id from ".$ecs->table('school')." where region_id=".$_REQUEST['area_id'];
        $id= $db->getOne($sql);
        $sql = 'SELECT * FROM ' . $ecs->table('school').'where parent_id=' . $id;
        $res = $db->getAll($sql);
        $smarty->assign('area',1);
        $smarty->assign('province',$res);
    }
    $sql = "select region_name, region_id from ".$ecs->table('school')." where region_type=0";
    $school_list = $db->getAll($sql);
    $smarty->assign('school_list',$school_list);

    /* 取得总数 */
    if($_REQUEST['area_id']){
        $sql = "select * from " . $ecs->table('school') . " where parent_id =". $_REQUEST['area_id'];
        $count=count($db->getAll($sql));
    }else{
        $count = school_count();
    }

    /* 取得每页记录数 */
    $size =  15;

    /* 计算总页数 */
    $page_count = ceil($count / $size);

    /* 取得当前页 */
    $page = isset($_REQUEST['page']) && intval($_REQUEST['page']) > 0 ? intval($_REQUEST['page']) : 1;

    $school = school_list($size, $page);
    $smarty->assign('school',  $school);
    /* 设置分页链接 */
    if($_REQUEST['region_id']!='')
    {
        $pager = get_pager('school.php', array('act' => 'list','region_id'=>$_REQUEST['region_id']), $count, $page, $size);
    }
    elseif($_REQUEST['area_id']!='')
    {
        $pager = get_pager('school.php', array('act' => 'list','area_id'=>$_REQUEST['area_id']), $count, $page, $size);
    }
    else
    {
        $pager = get_pager('school.php', array('act' => 'list'), $count, $page, $size);
    }
    $smarty->assign('pager', $pager);

    $affiliate = unserialize($GLOBALS['_CFG']['affiliate']);
    $smarty->assign('affiliate', $affiliate);
    $smarty->assign('type',         0);
    $smarty->assign('cfg',          $_CFG);
    $smarty->assign('helps',        get_shop_help()); // 网店帮助
    $smarty->assign('page_title',          $position['title']);                    // 页面标题
    $smarty->assign('ur_here',             $position['ur_here']);                  // 当前位置
    $smarty->display('school.dwt');
}
elseif($_REQUEST['act']=='addr'){
    //echo 123;
    $id=$_REQUEST['addr_id'];
    if(empty($id)){
        echo '参数不正确';
        exit;
    }else{
        $sql = 'SELECT * FROM ' . $ecs->table('school').'where parent_id=' . $id;
        $res = $db->getAll($sql);

        $smarty->assign('province',$res);
        //$result['content'] = '市';
        echo  $smarty->fetch('library/city.lbi');
    }

    //die($json->encode($result));

//
//    $sql = "select * from " . $ecs->table('school') . " where parent_id = $addr_id";
//    $city = $db->getAll($sql);  //省下面所有市
//    $school = array();
//    $schools = array();
//    foreach($city as $k => $c){
//        $schools[] = get_schools(2, $c['region_id']);
//    }
//    foreach($schools as $vo){
//        foreach($vo as $v){
//            $school[]=$v;
//        }
//    }
//    $count=count($school);
//    $size =  15;
//    $page_count = ceil($count / $size);
//    $page = isset($_REQUEST['page']) && intval($_REQUEST['page']) > 0 ? intval($_REQUEST['page']) : 1;
//
//    $school = school_list($size, $page);
//    $smarty->assign('school',  $school);
//
//    /* 设置分页链接 */
//    if($_REQUEST['addr_id']!=''){
//        $pager = get_pager('school.php', array('act' => 'list','addr_id'=>$_REQUEST['addr_id']), $count, $page, $size);
//    }
//    else{
//        $pager = get_pager('school.php', array('act' => 'list'), $count, $page, $size);
//    }
//    $smarty->assign('pager', $pager);
//    $affiliate = unserialize($GLOBALS['_CFG']['affiliate']);
//    $smarty->assign('affiliate', $affiliate);
//    $smarty->assign('type',         0);
//    $smarty->assign('cfg',          $_CFG);
//    $smarty->assign('helps',        get_shop_help()); // 网店帮助
//    $smarty->assign('page_title',          $position['title']);                    // 页面标题
//    $smarty->assign('ur_here',             $position['ur_here']);                  // 当前位置
//    $smarty->display('school.dwt');
}
elseif($_REQUEST['act']=='show'){
    $id = $_REQUEST['id'];
    $sql= "select c.region_name ,sum(s.rund_money), c.add_money, c.img_logo, c.number from ".$GLOBALS['ecs']->table('school')." as c left join ".$GLOBALS['ecs']->table('order_info')." as s on c.region_id= s.school_name where c.region_id=".$id;
    $school = $GLOBALS['db']->getRow($sql);

    $smarty->assign('school_name',$school['region_name']);
    $smarty->assign('img_logo',$school['img_logo']);
    $smarty->assign('number',$school['number']);
    $smarty->assign('region_id',$id);
    $smarty->assign('rund_money',$school['sum(s.rund_money)']+$school['add_money']);

    $sql1 = "select c.sent_time, c.title, c.message, c.is_show, s.user_name from ".$GLOBALS['ecs']->table('message')." as c left join ".$GLOBALS['ecs']->table('users')." as s on c.sender_id=s.user_id where c.school_id=".$id." and c.parent_id =0 and c.is_show=1";
    $count=count($GLOBALS['db']->getAll($sql1));
    $size=10;
    $page_count = ceil($count / $size);
    /* 取得当前页 */
    $page = isset($_REQUEST['page']) && intval($_REQUEST['page']) > 0 ? intval($_REQUEST['page']) : 1;

    $pager = get_pager('school.php', array('act' => 'show','id'=>$id), $count, $page, $size);
    $message = message_list($size, $page);


    $smarty->assign('message',$message);
    $smarty->assign('pager', $pager);
    $affiliate = unserialize($GLOBALS['_CFG']['affiliate']);
    $smarty->assign('affiliate', $affiliate);
    $smarty->assign('type',         0);
    $smarty->assign('cfg',          $_CFG);
    $smarty->assign('helps',        get_shop_help()); // 网店帮助
    $smarty->assign('page_title',          $position['title']);                    // 页面标题
    $smarty->assign('ur_here',             $position['ur_here']);                  // 当前位置

    $sql = "select * from ".$ecs->table('message')." where school_id=".$id." and parent_id !=0";
    $list = $db->getAll($sql);
    $smarty->assign('read_message',$list);
    $smarty->display('school_show.dwt');
}
elseif($_REQUEST['act']=='message'){
    $school_id = $_REQUEST["id"];
    $uid=empty($_SESSION['user_id']) ? "" : $_SESSION['user_id'];
    $title=empty($_REQUEST['title']) ? "" : $_REQUEST['title'];
    $message=empty($_REQUEST['message']) ? "" : $_REQUEST['message'];
    if($uid == ''){
        show_message('尚未登陆，请先登录！',"点击登陆",'user.php?act=login');
    }
    if($title == ''){
        show_message('标题不能为空！',"返回上一层",'school.php?act=show&id='.$school_id);
    }
    if($message == ''){
        show_message('内容不能为空！',"返回上一层",'school.php?act=show&id='.$school_id);
    }
    $time = date("Y-d-m H:i:s",time());
    $sql = "insert into ".$GLOBALS['ecs']->table('message')." (sender_id,sent_time,title,message,school_id) values(".$uid.",'".$time."','".$title."','".$message."',".$school_id.")";
    $GLOBALS['db']->query($sql);
    $url = 'school.php?act=show&id='.$school_id;
    ecs_header("Location:$url");
    //echo "ok";die;
}
function get_schools($type = 0, $parent = 0)
{
    $sql = 'SELECT region_id, region_name FROM ' . $GLOBALS['ecs']->table('school') .
        " WHERE region_type = '$type' AND parent_id = '$parent'";
   return $GLOBALS['db']->GetAll($sql);
}
/**
 * 取得学校数量
 * @return  int
 */
function school_count()
{
    $sql = "select COUNT(*) from ".$GLOBALS['ecs']->table('school')." where region_type=2";
    return $GLOBALS['db']->getOne($sql);
}

/**
 * 取得某页的学校
 * @param   int     $size   每页记录数
 * @param   int     $page   当前页
 * @return  array
 */
function school_list($size, $page)
{
    $idx=0;
    $school=array();
    if($_REQUEST['area_id']){
        $sql = "select * from ".$GLOBALS['ecs']->table('school')." where region_type=2 and parent_id=".$_REQUEST['area_id'];
    }else{
        $sql = "select * from ".$GLOBALS['ecs']->table('school')." where region_type=2";
    }

    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $school[$idx]['region_id'] = $row['region_id'];
        $sql= "select sum(s.rund_money) from ".$GLOBALS['ecs']->table('school')." as c left join ".$GLOBALS['ecs']->table('order_info')." as s on c.region_id= s.school_name where c.region_id=".$row['region_id'];
        $school[$idx]['total_money'] = $row['add_money']+($GLOBALS['db']->getOne($sql));
        $school[$idx]['region_name'] = $row['region_name'];
        $school[$idx]['img_logo'] = $row['img_logo'];
        $school[$idx]['add_money'] = $row['add_money'];
        $school[$idx]['overall_money'] = $row['overall_money'];
        $school[$idx]['number'] = $row['number'];
        $idx++;
    }
    return $school;
}
function message_list($size, $page)
{
    $id = $_REQUEST['id'];
    $idx=0;
    $message=array();
    $sql = "select c.sent_time, c.title, c.message, c.message_id, s.user_name from ".$GLOBALS['ecs']->table('message')." as c left join ".$GLOBALS['ecs']->table('users')." as s on c.sender_id=s.user_id where c.school_id=".$id." and c.parent_id=0 and c.is_show=1";
    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $message[$idx]['message'] = $row['message'];
        $message[$idx]['message_id'] = $row['message_id'];
        $message[$idx]['user_name'] = $row['user_name'];
        $message[$idx]['title'] = $row['title'];
        $message[$idx]['sent_time'] = $row['sent_time'];
        $idx++;
    }
    return $message;
}

/* 二分法分类 */
function getSelectChild($listarr,$pid,$low,$hight){
    $tr = '';
    $mid =intval(($low+$hight)/2);
    while($hight>=$low){
        $mid =intval(($low+$hight)/2);
        if($pid > $listarr[$mid]['parent_id']){//父id大于中间值 向下查
            $low = $mid+1;
        }else if($pid < $listarr[$mid]['parent_id']){//  父id小于中间值 向上查找
            $hight = $mid-1;
        }else if($pid == $listarr[$mid]['parent_id']){//  父id等于于中间值 即向上查找 又得向下查找
            $n = $listarr[$mid]['region_type']; //按’，‘统计层次数
            $h =  str_repeat(" ",$n-1)."<img src='admin/images/menu_minus.gif' id='icon_".($listarr[$mid]['region_type']-1)."_".$listarr[$mid]['region_id']."' width='9' height='9' border='0' style='margin-left:".$listarr[$mid]['region_type']."em' onclick='rowClicked(this)'/>";
            $tr .= "<tr class='".($listarr[$mid]['region_type']-1)."' id='".($listarr[$mid]['region_type']-1)."_".$listarr[$mid]['region_id']."'><td>".$h."<span style='font-size:12px;font-weight:bold'>".$listarr[$mid]['region_name']."</span>";
            if($listarr[$mid]['region_type']==1){
                $tr .="</td></tr>";
            }else{
                $tr .="<span style='margin-left:88px'> <a href='school.php?act=show&id=".$listarr[$mid]['region_id']."'>查看</a>
           </span></td></tr>";
            }
            $tr .= getSelectChild($listarr,$listarr[$mid]['region_id'],0,count($listarr)-1);

            if(($mid+1) < count($listarr)){
                if($pid == $listarr[$mid+1]['parent_id']){//当下一个数组的父id等于所有查找的pid时遍历出下面数组中所有等于pid的数组
                    for($i=$mid+1;$i<=$hight;$i++){
                        if($pid==$listarr[$i]['parent_id']){
                            $n = $listarr[$mid]['region_type']; //按’，‘统计层次数
                            $h =  str_repeat(" ",$n-1)."<img src='admin/images/menu_minus.gif' id='icon_".($listarr[$i]['region_type']-1)."_".$listarr[$i]['region_id']."' width='9' height='9' border='0' style='margin-left:".$listarr[$i]['region_type']."em' onclick='rowClicked(this)'/>";
                            $tr .= "<tr class='".($listarr[$i]['region_type']-1)."' id='".($listarr[$i]['region_type']-1)."_".$listarr[$i]['region_id']."'><td>".$h."<span style='font-size:12px;font-weight:bold'>".$listarr[$i]['region_name']."</span>";
                               if($listarr[$i]['region_type']==1){
                                   $tr .="</td></tr>";
                               }else{
                                   $tr .="<span style='margin-left:88px'> <a href='school.php?act=show&id=".$listarr[$i]['region_id']."'>查看</a></span></td></tr>";
                               }
                            $tr .= getSelectChild($listarr,$listarr[$i]['region_id'],0,count($listarr)-1);
                        }
                        else{
                            break;
                        }
                    }
                }
                if($pid == $listarr[$mid-1]['parent_id']){ //当上一个数组的父id等于所有查找的pid时遍历出上面数组中所有等于pid的数组
                    for($i=$mid-1;$i>=$low;$i--){
                        if($pid==$listarr[$i]['parent_id']){
                            $n =$listarr[$mid]['region_type']; //按’，‘统计层次数
                            $h =  str_repeat(" ",$n-1)."<img src='admin/images/menu_minus.gif' id='icon_".($listarr[$i]['region_type']-1)."_".$listarr[$i]['region_id']."' width='9' height='9' border='0' style='margin-left:".$listarr[$i]['region_type']."em' onclick='rowClicked(this)'/>";
                            $tr .= "<tr class='".($listarr[$i]['region_type']-1)."' id='".($listarr[$i]['region_type']-1)."_".$listarr[$i]['region_id']."'><td>".$h."<span style='font-size:12px;font-weight:bold'>".$listarr[$i]['region_name']."</span>";
                            if($listarr[$i]['region_type']==1){
                                $tr .="</td></tr>";
                            }else{
                                $tr .="<span style='margin-left:88px'> <a href='school.php?act=show&id=".$listarr[$i]['region_id']."'>查看</a></span></td></tr>";
                            }
                            $tr .= getSelectChild($listarr,$listarr[$i]['region_id'],0,count($listarr)-1);
                        }
                        else{
                            break;
                        }
                    }
                }
            }
            return $tr;
        }
    }
}
?>